<template>
  <div class="wrapper">
    <div class="app-loading-container">
      <canvas ref="particleCanvas" class="particle-canvas"></canvas>
      <div class="loading-text">
        <span class="star">✦</span>
        <span class="star">✦</span>
        <span class="star">✦</span>
        <span class="star">✦</span>
        <span class="star">✦</span>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, onBeforeUnmount } from 'vue'

// 粒子加载动画组件
interface Particle {
  x: number
  y: number
  size: number
  speedX: number
  speedY: number
  color: string
  alpha: number
}

const particleCanvas = ref<HTMLCanvasElement | null>(null)
let ctx: CanvasRenderingContext2D | null = null
let particles: Particle[] = []
let animationFrameId: number
let canvasWidth = 0
let canvasHeight = 0
let mouseX = 0
let mouseY = 0
let hue = 200 // 初始色相值

// 创建粒子
const createParticles = () => {
  const particleCount = 100
  particles = []

  for (let i = 0; i < particleCount; i++) {
    particles.push({
      x: Math.random() * canvasWidth,
      y: Math.random() * canvasHeight,
      size: Math.random() * 5 + 1,
      speedX: Math.random() * 2 - 1,
      speedY: Math.random() * 2 - 1,
      color: `hsl(${hue + Math.random() * 30}, 100%, 70%)`,
      alpha: Math.random() * 0.8 + 0.2
    })
  }
}

// 更新粒子
const updateParticles = () => {
  hue = (hue + 0.5) % 360 // 缓慢改变色相

  particles.forEach((particle) => {
    // 更新位置
    particle.x += particle.speedX
    particle.y += particle.speedY

    // 边界检测
    if (particle.x < 0 || particle.x > canvasWidth) {
      particle.speedX *= -1
    }

    if (particle.y < 0 || particle.y > canvasHeight) {
      particle.speedY *= -1
    }

    // 鼠标交互 - 当粒子靠近鼠标时改变方向
    const dx = mouseX - particle.x
    const dy = mouseY - particle.y
    const distance = Math.sqrt(dx * dx + dy * dy)

    if (distance < 100) {
      const angle = Math.atan2(dy, dx)
      const force = (100 - distance) / 1500

      particle.speedX -= Math.cos(angle) * force
      particle.speedY -= Math.sin(angle) * force

      // 靠近鼠标的粒子颜色更亮
      particle.color = `hsl(${hue + Math.random() * 30}, 100%, ${70 + (100 - distance) / 4}%)`
    } else {
      // 远离鼠标的粒子恢复原来的颜色
      if (Math.random() > 0.99) {
        particle.color = `hsl(${hue + Math.random() * 30}, 100%, 70%)`
      }
    }

    // 限制速度
    particle.speedX = Math.max(-2, Math.min(2, particle.speedX))
    particle.speedY = Math.max(-2, Math.min(2, particle.speedY))
  })
}

// 绘制粒子
const drawParticles = () => {
  if (!ctx) return

  // 清空画布
  ctx.clearRect(0, 0, canvasWidth, canvasHeight)

  // 绘制粒子
  particles.forEach((particle) => {
    ctx!.save()
    ctx!.globalAlpha = particle.alpha
    ctx!.fillStyle = particle.color
    ctx!.beginPath()
    ctx!.arc(particle.x, particle.y, particle.size, 0, Math.PI * 2)
    ctx!.fill()

    // 绘制粒子之间的连线
    particles.forEach((otherParticle) => {
      const dx = particle.x - otherParticle.x
      const dy = particle.y - otherParticle.y
      const distance = Math.sqrt(dx * dx + dy * dy)

      if (distance < 100) {
        ctx!.beginPath()
        ctx!.strokeStyle = particle.color
        ctx!.globalAlpha = ((100 - distance) / 100) * 0.5 * particle.alpha
        ctx!.lineWidth = 0.5
        ctx!.moveTo(particle.x, particle.y)
        ctx!.lineTo(otherParticle.x, otherParticle.y)
        ctx!.stroke()
      }
    })

    ctx!.restore()
  })
}

// 动画循环
const animate = () => {
  updateParticles()
  drawParticles()
  animationFrameId = requestAnimationFrame(animate)
}

// 处理鼠标移动
const handleMouseMove = (e: MouseEvent) => {
  if (!particleCanvas.value) return

  const rect = particleCanvas.value.getBoundingClientRect()
  mouseX = e.clientX - rect.left
  mouseY = e.clientY - rect.top
}

// 处理触摸移动
const handleTouchMove = (e: TouchEvent) => {
  if (!particleCanvas.value || !e.touches[0]) return

  const rect = particleCanvas.value.getBoundingClientRect()
  mouseX = e.touches[0].clientX - rect.left
  mouseY = e.touches[0].clientY - rect.top
}

// 调整画布大小
const resizeCanvas = () => {
  if (!particleCanvas.value || !ctx) return

  const container = particleCanvas.value.parentElement
  if (!container) return

  canvasWidth = container.clientWidth
  canvasHeight = container.clientHeight

  particleCanvas.value.width = canvasWidth
  particleCanvas.value.height = canvasHeight

  // 重新创建粒子
  createParticles()
}

onMounted(() => {
  if (!particleCanvas.value) return

  ctx = particleCanvas.value.getContext('2d')
  if (!ctx) return

  // 设置画布大小
  resizeCanvas()

  // 创建粒子
  createParticles()

  // 开始动画
  animate()

  // 添加事件监听
  window.addEventListener('resize', resizeCanvas)
  window.addEventListener('mousemove', handleMouseMove)
  window.addEventListener('touchmove', handleTouchMove)
})

onBeforeUnmount(() => {
  // 清理
  cancelAnimationFrame(animationFrameId)
  window.removeEventListener('resize', resizeCanvas)
  window.removeEventListener('mousemove', handleMouseMove)
  window.removeEventListener('touchmove', handleTouchMove)
})
</script>

<style scoped lang="scss">
.wrapper {
  position: relative;
  display: flex;
  height: 100vh;
  overflow: hidden;
  background: linear-gradient(135deg, #0f1215 0%, #1a1f25 100%);
  align-items: center;
  justify-content: center;
}

.app-loading-container {
  position: relative;
  z-index: 1;
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100%;
  align-items: center;
  justify-content: center;
}

.particle-canvas {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

.loading-text {
  position: relative;
  display: flex;
  margin-top: 20px;
  font-size: 18px;
  font-weight: 500;
  letter-spacing: 8px;
  color: rgb(255 255 255 / 80%);
  text-shadow: 0 2px 5px rgb(0 0 0 / 50%);
  justify-content: center;
}

.star {
  animation: twinkle 1.5s ease-in-out infinite alternate;
}

.star:nth-child(2) {
  animation-delay: 0.3s;
}

.star:nth-child(3) {
  animation-delay: 0.6s;
}

.star:nth-child(4) {
  animation-delay: 0.9s;
}

.star:nth-child(5) {
  animation-delay: 1.2s;
}

@keyframes twinkle {
  0% {
    color: rgb(255 255 255 / 60%);
    text-shadow: 0 0 5px rgb(255 255 255 / 30%);
    opacity: 0.2;
    transform: scale(0.8);
  }

  50% {
    color: rgb(255 255 255 / 100%);
    text-shadow:
      0 0 10px rgb(255 255 255 / 80%),
      0 0 20px rgb(255 255 255 / 50%);
    opacity: 1;
    transform: scale(1.2);
  }

  100% {
    color: rgb(255 255 255 / 60%);
    text-shadow: 0 0 5px rgb(255 255 255 / 30%);
    opacity: 0.2;
    transform: scale(0.8);
  }
}
</style>
